<html>
<head>
<link rel="shortcut icon" href="./favicon.ico">
<link rel="stylesheet" type="text/css" href="./style.css">
<link rel="canonical" href="./word_count_function.html">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Given the width of a bit vector, and the width of a word, returns the count of words needed to contain the bit vector, even if the last word will be partially full. You must compute any pad bits externally using [word_pad](./word_pad_function.html).">
<title>word count function</title>
</head>
<body>

<p class="inline bordered"><b><a href="./word_count_function.vh">Source</a></b></p>
<p class="inline bordered"><b><a href="./legal.html">License</a></b></p>
<p class="inline bordered"><b><a href="./index.html">Index</a></b></p>

<h1>Word count needed to pack/unpack a bit vector</h1>
<p>Given the width of a bit vector, and the width of a word, returns the count
 of words needed to contain the bit vector, even if the last word will be
 partially full. You must compute any pad bits externally using
 <a href="./word_pad_function.html">word_pad</a>.</p>
<p>Pass the function values which, at elaboration time, are either constants
 or expressions which evaluate to a constant. Then use the return value as an
 integer for a localparam, genvar, etc...</p>
<p>Since this is an included file, it must be idempotent. (defined only once globally)</p>

<pre>
`ifndef WORDCOUNT_FUNCTION
`define WORDCOUNT_FUNCTION

function integer word_count;
    input integer bit_vector_width;
    input integer word_width;
          integer word_count_raw;
    begin
        word_count_raw = bit_vector_width / word_width;
        word_count = (bit_vector_width % word_width == 0) ? word_count_raw : word_count_raw + 1;
    end
endfunction

`endif
</pre>

<hr>
<p><a href="./index.html">Back to FPGA Design Elements</a>
<center><a href="https://fpgacpu.ca/">fpgacpu.ca</a></center>
</body>
</html>

